using Cysharp.Threading.Tasks;
using System.Diagnostics;
using UnityEngine.Networking;

namespace Framework.Helper
{
    public static class HttpHelper
    {
        public static async UniTask<string> HttpGet(string url, int timeout = 10)
        {
            UnityWebRequest request = UnityWebRequest.Get(url);
            request.timeout = timeout;
            await request.SendWebRequest();

            int errorCode = 200;
            string detail = "";
            if (request.result != UnityWebRequest.Result.Success)
            {
                errorCode = 404;
                detail = request.downloadHandler.error;
            }
            else
            {
                detail = request.downloadHandler.text;
            }
            
            return "{\"errorCode\":" + errorCode + ", \"detail\":" + detail + "}";
        }
    }
}